﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TiaoTiaoCode.MultiLanguage
{
    /// <summary>
    /// Json化语言包文件帮助类
    /// </summary>
    internal class JsonLangHelper
    {
        /// <summary>
        /// 读取语言Json文件为字典
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Dictionary<string, string> LoadJsonLang(string path)
        {
            var files = Directory.GetFiles(path);
            if (files.Length <= 0) return new Dictionary<string, string>();

            var result = new Dictionary<string, string>();
            foreach (var file in files)
            {
                var content = File.ReadAllText(file);
                var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
                if (dic == null) continue;
                foreach (var kv in dic)
                {
                    var v = string.IsNullOrWhiteSpace(kv.Value) ? kv.Key : kv.Value;

                    if (result.ContainsKey(kv.Key))
                        result[kv.Key] = v;
                    else
                        result.Add(kv.Key, v);
                }
            }
            return result;
        }
    }
}
